package com.wp.file.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wp.file.common.BaseContext;
import com.wp.file.common.R;
import com.wp.file.pojo.Warehouse;
import com.wp.file.pojo.vo.Share;
import com.wp.file.service.WarehouseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/share")
public class ShareController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WarehouseService warehouseService;

    /**
     * 加入分享
     *
     * @param
     * @return
     */
    @PostMapping("/setting")
    public R<String> setting(@RequestBody Warehouse warehouse) {
        //加入分享就是要把warehouse文件的一些东西拿过来 然后过期时间设置一下存入redis里面
        Share share = new Share();
        share.setShareId(warehouse.getId());
        share.setName(warehouse.getDisplayName());
        share.setSize(warehouse.getDisplaySize());
        //统一设置为一天时间
        LocalDateTime tomorrow = LocalDateTime.now().plusDays(1); // 获取当前日期和时间
        share.setExpireTime(tomorrow);
        //存入redis
        redisTemplate.opsForHash().put(BaseContext.getCurrentId().toString(), share.getShareId().toString(), share);
        log.info(BaseContext.getCurrentId().toString() + " " + share.getShareId() + " " + share.toString());
        return R.success("开启文件分享成功!");
    }

    /**
     * 获取分享列表
     *
     * @return
     */
    @GetMapping("/getlist")
    public R<List<Share>> getList() {
        List<Share> values = redisTemplate.opsForHash().values(BaseContext.getCurrentId().toString());
        values.sort(Comparator.comparing(Share::getExpireTime).reversed());
        return R.success(values);
    }

    /**
     * 获取某个文件的分享页面信息
     *
     * @return
     */
    @GetMapping("/link")
    public R<Share> getlink(Long warehouseId) {
        Share share = (Share) redisTemplate.opsForHash().get(BaseContext.getCurrentId().toString(), warehouseId.toString());
        if (share.getExpireTime().compareTo(LocalDateTime.now()) < 0) {
            // share 已过期
            return R.error("已过期");
        }
        return R.success(share);
    }

    @PostMapping("/save")
    public R<String> savefile(@RequestBody Map map) {
        Long shareId = Long.parseLong((String)map.get("shareId"));
        Warehouse warehouseShare = warehouseService.getById(shareId);
        LambdaQueryWrapper<Warehouse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Warehouse::getUserId, BaseContext.getCurrentId());
        lqw.eq(Warehouse::getFileId, warehouseShare.getFileId());
        lqw.eq(Warehouse::getPath, "/");
        Warehouse one = warehouseService.getOne(lqw);
        if (one != null) {
            //已经存在根目录的这个文件
            return R.error("已保存该文件!");
        }
        Warehouse warehouseNew = new Warehouse();
        warehouseNew.setUserId(BaseContext.getCurrentId());
        warehouseNew.setSort(warehouseShare.getSort());
        warehouseNew.setDisplayName(warehouseShare.getDisplayName());
        warehouseNew.setPath("/");
        warehouseNew.setFileId(warehouseShare.getFileId());
        warehouseNew.setDisplaySize(warehouseShare.getDisplaySize());
        warehouseNew.setUpdateTime(warehouseShare.getUpdateTime());
        warehouseService.save(warehouseNew);
        return R.success("保存完成!该文件保存在/"+warehouseNew.getDisplayName());
    }

}
